/**
 *kuwang
 * 2019年9月25日
 */
package com.yidu.privilege.Service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.yidu.privilege.Dao.ModuleAndroleMapper;
import com.yidu.privilege.Dao.ModuleDao;
import com.yidu.privilege.Domain.Module;
import com.yidu.privilege.Service.ModuleService;
import com.yidu.privilege.vo.ModuleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yidu.privilege.Utils.Pages;

import javax.annotation.Resource;

/**
 * @author kuwang
 *
 * 2019年9月25日 上午9:14:10
 */
@Service
public class ModuleServiceimpl implements ModuleService {
	@Resource
	private ModuleDao moduleDao;
	@Resource
    ModuleAndroleMapper moduleAndroleMapper;

	/* (non-Javadoc)
	 * @see com.yidu.Serice.ModuleService#findAll()
	 */
	@Override
	public List<ModuleVo> findAll(ModuleVo mod, Pages pa) {
		Module md=new Module();
		System.out.println(pa.getLimit()+"=======s===="+pa.getPage());
		BeanUtils.copyProperties(mod, md);
		List<Module> list=moduleDao.findAlls(md,pa);
		List<ModuleVo> list2=new ArrayList<ModuleVo>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Module module = (Module) iterator.next();
			ModuleVo m=new ModuleVo();
			BeanUtils.copyProperties(module, m);
			if(module.getModSuperiors()!=null) {
				m.setFumodName(moduleDao.findbyid(module.getModSuperiors()).getModName());
			
			}
			list2.add(m);
		}
		
		return list2;
	}

	/* (non-Javadoc)
	 * @see com.yidu.Serice.ModuleService#savaOrupdate(com.yidu.vo.ModuleVo)
	 */



	/* (non-Javadoc)
	 * @see com.yidu.Serice.ModuleService#findbyid(java.lang.Integer)
	 */
	@Override
	public ModuleVo findbyid(Integer id) {
		Module mo=moduleDao.findbyid(id);
		ModuleVo vo=new ModuleVo();
		BeanUtils.copyProperties(mo, vo);
		if(vo.getModSuperiors()!=null) {
		vo.setFumodName(moduleDao.findbyid(vo.getModSuperiors()).getModName());
		}
		return vo;
	}

	@Override
	public List findcha(Integer name) {
		List list=moduleDao.findcha(name);

		List<Map<String, Object>> lsit2=new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map=new HashMap<String, Object>();
			List<Map<String, Object>> lsit3=new ArrayList<Map<String, Object>>();
			String str=(String) ((Map<String,Object>)list.get(i)).get("funame");
			Map<String, Object> map2;
			map2=new HashMap<String, Object>();
			map2.put("name",(String) ((Map<String,Object>)list.get(i)).get("ziname"));
			map2.put("url",(String) ((Map<String,Object>)list.get(i)).get("loaction"));
			lsit3.add(map2);
			for (int j = i+1; j < list.size(); j++) {
				String str2=(String) ((Map<String,Object>)list.get(j)).get("funame");

				if(str.equals(str2)) {
					map2=new HashMap<String, Object>();
					map2.put("name",(String) ((Map<String,Object>)list.get(j)).get("ziname"));
					map2.put("url",(String)((Map<String,Object>)list.get(j)).get("loaction"));
					lsit3.add(map2);
					list.remove(j);
					j--;
				}
			}
			map.put("name",str);
			map.put("icon","");
			map.put("url","");
			map.put("hidden",false);
			map.put("list",lsit3);
			lsit2.add(map);
		}

		return lsit2;
	}
	public Long count(ModuleVo mod) {
		Module md=new Module();
		BeanUtils.copyProperties(mod, md);
		return (long) moduleDao.count(md);
	}

	@Override
	public int addOrupdate(ModuleVo mod) {
		Module md=new Module();
		BeanUtils.copyProperties(mod, md);
		if(mod.getModSuperiors()!=null) {

			md.setModSuperiors(mod.getModSuperiors());
		}
		md.setModState("0");
		if(StringUtils.isEmpty(mod.getModId())){
			return moduleDao.add(md);
		}else{
			return moduleDao.update(md);
		}


	}

	@Override
	public int delete(Integer modId) {
		Module mo=moduleDao.findbyid(modId);
		mo.setModState("1");
		if(mo.getModSuperiors()==null) {
			List<Module> list=moduleDao.findAlld(mo.getModId());
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Module module = (Module) iterator.next();
				module.setModState("1");
				moduleDao.update(module);
			}
		}
		return moduleDao.update(mo);
	}



	@Override
	public List findAll(Integer id) {
		List list=moduleDao.findAll();
		List listnull=moduleDao.findnull();
		List<Map<String, Object>> lsit2=new ArrayList<Map<String, Object>>();
		Map<String,Object> ob = new HashMap<String, Object>();
		ob.put("title","无");
		ob.put("id",null);
		ob.put("children",null);
		lsit2.add(ob);
		for (Iterator iterator = listnull.iterator(); iterator.hasNext();) {
			Map<String,Object> object = (Map<String,Object>) iterator.next();
			if(id==(Integer)object.get("modid")) {
				continue;
			}
			Map<String, Object> map=new HashMap<String, Object>();
			String str=(String) object.get("modname");
			List<Map<String, Object>> lsit3=new ArrayList<Map<String, Object>>();
			map.put("title",str);
			map.put("id",(Integer)object.get("modid"));
			map.put("children",lsit3);
			lsit2.add(map);
		}

		
		for (int i = 0; i < list.size(); i++) {
			if(id==(Integer) ((Map<String,Object>)list.get(i)).get("fuid")) {
				continue;
			}
			Map<String, Object> map=new HashMap<String, Object>();
			List<Map<String, Object>> lsit3=new ArrayList<Map<String, Object>>();
			String str=(String) (((Map<String,Object>)list.get(i))).get("funame");
			Map<String, Object> map2;
			if(id==(Integer) (((Map<String,Object>)list.get(i)).get("ziid"))){
				continue;
			}
			
			map2=new HashMap<String, Object>();
			map2.put("title",(String) (((Map<String,Object>)list.get(i))).get("ziname"));
			map2.put("id",(Integer) (((Map<String,Object>)list.get(i)).get("ziid")));
			lsit3.add(map2);

			for (int j = i+1; j < list.size(); j++) {
				if(id==(Integer) (((Map<String,Object>)list.get(j)).get("ziid"))) {
					continue;
				}

				String str2=(String) ((Map<String,Object>)list.get(j)).get("funame");


				if(str.equals(str2)) {
					map2=new HashMap<String, Object>();
					map2.put("title",(String) (((Map<String,Object>)list.get(j))).get("ziname"));
					map2.put("id",(Integer) (((Map<String,Object>)list.get(j)).get("ziid")));
					lsit3.add(map2);
					list.remove(j);
					j--;
				}
			}
			map.put("title",str);
			map.put("id",(Integer) ((Map<String,Object>)list.get(i)).get("fuid"));
			map.put("children",lsit3);
			lsit2.add(map);
		}


		return lsit2;
	};
	
	public List findAll2(Integer id) {
		List list=moduleDao.findAll();
		List listnull=moduleDao.findnull();
		List listmd=moduleAndroleMapper.find(id);
		
		List<Map<String, Object>> lsit2=new ArrayList<Map<String, Object>>();
		for (Iterator iterator = listnull.iterator(); iterator.hasNext();) {
			
			Map<String,Object> object = (Map<String,Object>) iterator.next();
			Map<String, Object> map=new HashMap<String, Object>();
			String str=(String) object.get("modname");
			List<Map<String, Object>> lsit3=new ArrayList<Map<String, Object>>();
			map.put("title",str);
			map.put("id",(Integer)object.get("modid"));
			for (Iterator iterator2 = listmd.iterator(); iterator2.hasNext();) {
				Integer fg = (Integer) iterator2.next();
				if(fg==(Integer)object.get("modid")) {
					map.put("checked",true);
					
				}
			}
			map.put("children",lsit3);
			lsit2.add(map);
		}

		
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map=new HashMap<String, Object>();
			List<Map<String, Object>> lsit3=new ArrayList<Map<String, Object>>();
			String str=(String) ((Map<String, Object>)list.get(i)).get("funame");
			Map<String, Object> map2;
			map2=new HashMap<String, Object>();
			map2.put("title",(String)((Map<String, Object>)list.get(i)).get("ziname"));
			map2.put("id",(Integer) ((Map<String, Object>)list.get(i)).get("ziid"));
			for (Iterator iterator7 = listmd.iterator(); iterator7.hasNext();) {
				Integer fg = (Integer) iterator7.next();
				if(fg==(Integer) ((Map<String, Object>)list.get(i)).get("ziid")) {
					map2.put("checked",true);
					
				}
			}
			lsit3.add(map2);


			for (int j = i+1; j < list.size(); j++) {


				String str2=(String) ((Map<String, Object>)list.get(j)).get("funame");


				if(str.equals(str2)) {
					map2=new HashMap<String, Object>();
					map2.put("title",(String) (((Map<String, Object>)list.get(j)).get("ziname")));
					map2.put("id",(Integer) (((Map<String, Object>)list.get(j)).get("ziid")));
					for (Iterator iterator6 = listmd.iterator(); iterator6.hasNext();) {
						Integer fg = (Integer) iterator6.next();
						if(fg==(Integer) (((Map<String, Object>)list.get(j)).get("ziid"))) {
							map2.put("checked",true);
							
						}
					}
					lsit3.add(map2);
					list.remove(j);
					j--;
				}
			}
			map.put("title",str);
			
			map.put("id",(Integer)((Map<String, Object>)list.get(i)).get("fuid"));
			map.put("children",lsit3);
			lsit2.add(map);
		}


		return lsit2;
	}

	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		
		String temp[] = ids.split(",");
		for (int i = 0; i < temp.length; i++) {
			String id = temp[i];
			if(StringUtils.isEmpty(id)) {
				continue ;
			}
			delete(Integer.valueOf(id));
		}
		
	};
}
